package com.yima.baseproj.module.app.fankui;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yima.baseproj.module.app.fankui.model.AppFankuiAddParamModel;
import com.yima.baseproj.module.app.fankui.model.AppFankuiSearchModel;
import com.yima.baseproj.module.app.user.AppUserEntity;
import com.yima.baseproj.module.app.user.AppUserService;
import com.yima.baseproj.config.exception.type.ApiException;
import com.yima.baseproj.module.app.fankui.model.AppFankuiSetStateParamModel;
import com.yima.baseproj.module.config.BaseController;
import com.yima.baseproj.module.config.utils.PageUtil;
import com.yima.baseproj.module.config.utils.PageVo;
import com.yima.baseproj.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author 一码
 * @createDate 2023/4/1 09:00
 * @contact 1424529270@qq.com
 */
@Api(tags = "意见反馈管理")
@RestController
@RequestMapping("/api/app/fankui")
public class AppFankuiController extends BaseController {

    @Resource
    private AppFankuiService appFankuiService;

    @Resource
    private AppUserService appUserService;

    @ApiOperation("查询分页")
    @GetMapping(value = "/findAllByPage")
    public Object findAllByPage(PageVo page, AppFankuiSearchModel searchVo) {
        Page<Map> pageData = appFankuiService.findByPage(PageUtil.initMpPage(page),searchVo);
        for (Map record : pageData.getRecords()) {
            record.put("filesShow",record.get("files").toString().split(","));
            AppUserEntity appUserEntity = appUserService.getById(record.get("userId").toString());
            if(appUserEntity == null){
                record.put("userPhone","");
            }else{
                record.put("userPhone", appUserEntity.getPhone());
            }
        }
        return pageData;
    }

    @ApiOperation("添加")
    @PostMapping(value = "/add")
    public void add(@Validated @RequestBody AppFankuiAddParamModel paramModel){
        AppFankuiEntity appFankuiEntity = new AppFankuiEntity();
        appFankuiEntity.setData(paramModel);
        appFankuiEntity.setUserId(SecurityUtil.getUserId());
        appFankuiService.save(appFankuiEntity);
    }

    @ApiOperation("修改状态")
    @PostMapping(value = "/editState")
    public void setState(@Validated @RequestBody AppFankuiSetStateParamModel paramModel){
        AppFankuiEntity fankuiEntity = appFankuiService.getById(paramModel.getId());
        if(fankuiEntity == null){
            throw new ApiException("数据不存在，请刷新重试");
        }
        fankuiEntity.setState(paramModel.getState());

        appFankuiService.saveOrUpdate(fankuiEntity);
    }
}
